<?php

namespace App\Admin\Controllers;

use App\Models\BrandSort;
use App\Models\ProductGoods;
use App\Models\AfterSales;
use App\Models\ProductSort;
use App\Util\Url;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Auth\Database\Administrator;

class ReimburseController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '售后列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AfterSales());
        $grid->disableExport();//禁止导出
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete(); //禁止行级删除路由
        });
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
//            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            //商品名称	商品类型
            $filter->like('product_name', __('商品名称'));
            $filter->equal('product_source', "商品类型")->select([1 => '工作室', 2 => "供应商"]);
        });
        $userid = auth()->user()->id ?? 0;
        $roleId = Administrator::find($userid)->roles()->first()->pivot->role_id; //获取角色id
        $where = [];
        if ($roleId != 1) {
            $where [] = ['product_add_user_id', '=', $userid];
        }
        $where [] = ['product_audit_status', '=', 2];//已通过
        $grid->model()->where($where)->orderByDesc('id');//只查看审核过的商品
        $grid->column('id', __('Id'))->sortable();
        $grid->column('product_name', __('商品名称'));
        $grid->column('product_image', __('商品图片'))->display(function () {
            return Url::getHttpHost() . 'upload/' . $this->product_image;
        })->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('product_retail_price', __('平台售价'));
        $grid->column('product_source', __('商品类型'))->display(function () {
            $option = [
                1 => '工作室',
                2 => '供应商',
            ];
            return $option[$this->product_source] ?? '无';
        });
        $grid->column('product_original_price', __('经销商售价'));
        $grid->column('product_stock', __('库存'));
        $grid->column('product_sales', __('已售数量'));
        $grid->column('created_at', __('创建时间'));

        $states = [
            'on' => ['value' => 0, 'text' => '待审核', 'color' => 'danger'],
            'off' => ['value' => 2, 'text' => '审核通过', 'color' => 'success'],
        ];
        $grid->column('product_audit_status', __('审核状态'))->switch($states);
        $states = [
            'on' => ['value' => 1, 'text' => '下架', 'color' => 'danger'],
            'off' => ['value' => 2, 'text' => '上架', 'color' => 'success'],
        ];
        $grid->column('product_racking', __('是否上架'))->switch($states);
        /*
        $grid->column('product_unit', __('商品单位'));
        $grid->column('product_brand_id', __('品牌类型'))->display(function () {
            return (new BrandSort())->getName($this->product_brand_id);
        });
        $grid->column('product_detail_image_one', __('商品详情一'))->display(function () {
            return Url::getHttpHost() . 'upload / ' . $this->product_detail_image_one;
        })->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('product_detail_image_two', __('商品详情二'))->display(function () {
            return Url::getHttpHost() . 'upload / ' . $this->product_detail_image_two;
        })->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('product_detail_image_three', __('商品详情三'))->display(function () {
            return Url::getHttpHost() . 'upload / ' . $this->product_detail_image_three;
        })->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('product_detail_image_four', __('商品详情四'))->display(function () {
            return Url::getHttpHost() . 'upload / ' . $this->product_detail_image_four;
        })->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('product_detail_image_five', __('商品详情五'))->display(function () {
            return Url::getHttpHost() . 'upload / ' . $this->product_detail_image_five;
        })->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('product_rests_image', __('其他'))->display(function () {
            return Url::getHttpHost() . 'upload / ' . $this->product_rests_image;
        })->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('product_cost_price', __('产品成本价'));
        $grid->column('product_label', __('产品标签'));
        $grid->column('product_coding', __('产品编码'));
        $grid->column('updated_at', __('更新时间'));
         */
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(AfterSales::findOrFail($id));
        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
        });
        $show->field('id', __('ID'));
        $show->field('product_name', __('商品名称'));
        $show->field('product_image', __('商品图片'))->as(function () {
            return $this->product_image;
        })->image(Url::getHttpHost() . 'upload/', 100, 100);

        $show->field('product_unit', __('商品单位'));
        $show->field('product_sort_id', __('商品分类'))->as(function () {
            return (new ProductSort())->getName($this->product_sort_id);
        });
        $show->field('product_brand_id', __('品牌'))->as(function () {
            return (new BrandSort())->getName($this->product_brand_id);
        });
        $show->field('product_original_price', __('经销商售价'));
        $show->field('product_retail_price', __('平台售价'));
        $show->field('product_cost_price', __('产品成本价'));
        $show->field('product_sales', __('已售数量'));
        $show->field('product_label', __('产品标签'));
        $show->field('product_coding', __('产品编码'));
        $show->field('product_detail_image_one', __('商品详情一'))->as(function () {
            return $this->product_detail_image_one;
        })->image(Url::getHttpHost() . 'upload/', 100, 100);

        $show->field('product_detail_image_two', __('商品详情二'))->as(function () {
            return $this->product_detail_image_two;
        })->image(Url::getHttpHost() . 'upload/', 100, 100);
        $show->field('product_detail_image_three', __('商品详情三'))->as(function () {
            return $this->product_detail_image_three;
        })->image(Url::getHttpHost() . 'upload/', 100, 100);
        $show->field('product_detail_image_four', __('商品详情四'))->as(function () {
            return $this->product_detail_image_four;
        })->image(Url::getHttpHost() . 'upload/', 100, 100);

        $show->field('product_detail_image_five', __('商品详情五'))->as(function () {
            return $this->product_detail_image_five;
        })->image(Url::getHttpHost() . 'upload/', 100, 100);

        /*$show->field('product_rests_image', __('其他'))->as(function () {
            return $this->product_rests_image;
        })->image(Url::getHttpHost() . 'upload / ', 100, 100);*/
        /*$show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));*/
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new AfterSales());
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->text('product_name', __('商品名称'))->rules('required');
        $form->image('product_image', __('商品图片'))->uniqueName()->removable()->rules('required');//单个可以
        $form->select('product_sort_id', '商品分类')->options((new ProductSort())->getOptions())->default(1)->rules('required');
        $form->number('product_stock', __('库存'))->rules('required');
        $form->number('product_sales', __('已售数量'))->default(0)->rules('required');
        $form->currency('product_retail_price', __('平台售价'))->symbol('￥')->rules('required');
        $form->currency('product_original_price', __('经销商售价'))->symbol('￥')->rules('required');
        $form->text('product_unit', __('商品单位'))->rules('required');
        $form->select('product_brand_id', '品牌分类')->options((new BrandSort())->getOptions())->default(1)->rules('required');
        $form->image('product_detail_image_one', __('商品详情一'))->uniqueName()->removable()->rules('required');
        $form->image('product_detail_image_two', __('商品详情二'))->uniqueName()->removable()->rules('required');
        $form->image('product_detail_image_three', __('商品详情三'))->uniqueName()->removable()->rules('required');
        $form->image('product_detail_image_four', __('商品详情四'))->uniqueName()->removable()->rules('required');
        $form->image('product_detail_image_five', __('商品详情五'))->uniqueName()->removable()->rules('required');
        $form->image('product_rests_image', __('其他'))->uniqueName()->removable();
        $form->currency('product_cost_price', __('产品成本价'))->symbol('￥')->rules('required');
        $form->text('product_label', __('产品标签'))->rules('required');
        $form->text('product_coding', __('产品编码'))->rules('required');
        $form->text('product_bar_code', __('产品条形码'))->rules('required');
        $form->text('product_param', __('产品参数'))->default("");

        $options = [0 => '待审核', 1 => '未通过', 2 => '审核通过'];
        $form->radio('product_audit_status', __('审核状态'))->options($options)->default(2)->stacked();

        $states = [
            'on' => ['value' => 1, 'text' => '下架', 'color' => 'danger'],
            'off' => ['value' => 2, 'text' => '上架', 'color' => 'success'],
        ];
        $form->switch('product_racking', __('是否上架'))->states($states)->default(2);

        $form->saving(function (Form $form) {
//            dd($form);
            $userid = auth()->user()->id ?? 0;
            $roleId = Administrator::find($userid)->roles()->first()->pivot->role_id; //获取角色id
            $source = 0;
            if ($roleId == 2) {
                $source = 2;
            }
            $form->model()->setAttribute('product_score', 5);//评分默认5分
            $form->model()->setAttribute('product_racking', 1);//是否上架(1:下架,2:上架)
            $form->model()->setAttribute('product_add_type', 2);//0:后端,1:医护端,2:供应商
            $form->model()->setAttribute('product_audit_status', 2);//审核状态(1:待审核，2:审核通过)
            $form->model()->setAttribute('product_source', $source);//1医护端(自营),2供应商
            $form->model()->setAttribute('product_add_user_id', $userid);//添加用户id
            $form->model()->setAttribute('product_audit_user_id', $userid);//经销不需要审核所以经销商审核id是用户自己id
        });
        return $form;
    }
}
